package com.paas.form.core.model;

import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.Config;
import com.paas.form.core.element.field.FieLdsModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public class ElCol extends ElementUI {

    private Integer _span;

    public ElCol(FieLdsModel fieLdsModel, int indentNum) {
        Config config = fieLdsModel.getConfig();
        this._span = config.getSpan();
        this.indentNum = indentNum;
    }

    @Override
    public void getElHtml(StringBuilder str) {

    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "<el-col${[span]}${[css_class]}${[css_style]}>";

        Map<String, Object> params = new HashMap<>();
        params.put("span", " :span=\"" + this._span + "\" ");
        params.put("css_style", StringUtil.isNotEmpty(this.css_style) ? " style=\"" + this.css_style + "\" " : null);
        params.put("css_class", StringUtil.isNotEmpty(this.css_class) ? " class=\"" + this.css_class + "\" " : null);
        params.put("event_click", StringUtil.isNotEmpty(this.event_click) ? " @click=\"" + this.event_click + "\" " : null);

        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext parserContext = new TemplateParserContext("${", "}");

        str.append(parser.parseExpression(codeTemplate, parserContext).getValue(params, String.class));
        str.append("\n");
    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + "</el-col>";
        str.append(codeTemplate);
        str.append("\n");
    }


}